// 导入发送邮件的第三方库
const { rejects } = require("assert")
const nodemailer = require("nodemailer")
const { resolve } = require("path")
// 导入utils
const { getVerificationCode } = require('./user.js')

// 邮箱参数设置
let transporter = nodemailer.createTransport({
    // host: 'smtp.163.com',
    // port: 465,   //qq邮件服务所占用的端口
    service: '163',     //邮箱类型 例如service:'163'
    secure: true,   //是否使用安全连接，对https协议的
    auth: {
        user: 'forestofmylife@163.com', //开启SMTP的邮箱，发件人
        pass: 'RWPQHGVLTIOQTOFJ'    // 授权密码
    }
})

// 发送邮件
exports.sentEmail = (emailRecipient) => {
    let verificationCode = getVerificationCode()
    let options = {
        from: 'forestofmylife@163.com',  //发送方
        to: `${ emailRecipient }`, //接收方
        subject: '江海与诗APP注册验证码',   //邮件主题
        html: `<div id="mailContentContainer" class="qmbox qm_con_body_content qqmail_webmail_only" style="opacity: 1;">
            <style>
                .qmbox body { width: 100%; }
                .qmbox p { margin: 30px 0; }
                .qmbox .verification-code { color: #FC8200; }
                .qmbox .gery-text { color: #AAAAAA; }
            </style>
            <p>亲爱的<a href="mailto:${ emailRecipient }" rel="noopener" target="_blank">${ emailRecipient }</a>：</p>
            <p>您好，非常感谢您使用江海与诗APP！您的验证码为</p>
            <p class="verification-code">${ verificationCode }</p>
            <p>为了保证您帐号的安全性，该验证码有效期为5分钟，如非本人操作，请忽略此消息!</p>
            <p class="gery-text">本邮件由系统自动发送，请勿回复！</p>
            <style type="text/css">.qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}</style>
        </div>`,    //html模板
    }
    return new Promise((resolve, rejected) => {
        transporter.sendMail(options, (err, info) => {
            resolve({err, info, verificationCode}) 
        })
    }) 
}